local BillboardExcel = require("excel.billboard")
local UnionExcel = require("excel.union")
local Msg = require("core.Msg")
local BillboardDefine = require("billboard.BillboardDefine")
local BillboardDB = require("billboard.BillboardDB")
local BRoleLogic = require("billboard.BRoleLogic")
local BillboardAim = require("billboard.BillboardAim")
local BattleLogic = require("battle.BattleLogic")
local RoleLogic = require("role.RoleLogic")
local UnionDBLogic = require("union.UnionDBLogic")
local CombatDefine = require("combat.CombatDefine")
local UnionExcel = require("excel.union")

function fontBoardUnionNet(net, union)
    local lv = math.min(union.lv, #UnionExcel.union)
    local config = UnionExcel.union[lv]

	net.unionUuid = union._id 
	net.name = union.name 
	net.lv = union.lv 
    net.maxCnt = config.maxCnt
    net.curCnt = union.curCnt
	net.zhandouli = union.zhandouli
	net.bannerID = union.bannerID
end

local function fontValuesShow(net, boardType, rankData)
	if boardType == BillboardDefine.TYPE_BATTLE then
		local battleID = rankData and rankData.value1 or 0
		local battleName, mapName = BattleLogic.getBattleName(battleID)
		net.value1 = mapName .. battleName
		net.value2 = ""
	else
		net.value1 = tostring(rankData and rankData.value1 or "")
		net.value2 = tostring(rankData and rankData.value2 or "")
	end
end

local function getBoardUuidByType(boardType, human)
	if boardType == BillboardDefine.TYPE_UNION then
		return human.db.unionUuid
	else
		return human.db._id
	end
end

local function getZhanliTypeByType(boardType)
	if boardType == BillboardDefine.TYPE_LIANYU then
		return CombatDefine.COMBAT_TYPE1
	end
end

function fontBoardNet(net, boardType, rank, rankData, uuid)
	net.rank = rank or 0	
	net.unionData[0] = 0	
	if boardType == BillboardDefine.TYPE_UNION then
		local union = UnionDBLogic.getUnion(rankData.uuid)
		local presidentUuid = uuid
		if union then
			net.unionData[0] = 1
			fontBoardUnionNet(net.unionData[1], union)
			presidentUuid = union.presidentUuid
		end
		RoleLogic.getRoleBaseByUuid(presidentUuid, net.roleBase)
	else
		if rankData and rankData.roleBase then
			RoleLogic.makeRoleBase(rankData.roleBase, net.roleBase)
		else
			local roleUuid = rankData and rankData.uuid or uuid
			RoleLogic.getRoleBaseByUuid(roleUuid, net.roleBase, getZhanliTypeByType(boardType))
		end
	end
	fontValuesShow(net, boardType, rankData)
end

-- 封装总览结构体
function fontMainNet(net, boardType, config, human)
	net.boardType = boardType
	net.boardName = config.boardName
	net.topData[0] = 0
	local rank = 1
	local rankData = BillboardDB.getRankData(boardType, rank)
	if rankData then
		net.topData[0] = 1
		fontBoardNet(net.topData[1], boardType, rank, rankData)
	end
	net.hasAim = BillboardAim.getAimIDsByType(boardType) and 1 or 0
	net.hasAimRed = BillboardAim.isAimRed(human, boardType) and 1 or 0
end

-- 发送排行榜总览列表
function sendMainList(human)
	local msgRet = Msg.gc.GC_BILLBOARD_MAINLIST
	msgRet.list[0] = 0
	for boardType, config in ipairs(BillboardExcel.board) do
		if config.isShow == 1 then
			msgRet.list[0] = msgRet.list[0] + 1
			local net = msgRet.list[msgRet.list[0]]
			fontMainNet(net, boardType, config, human)
		end
	end
	--Msg.trace(msgRet)
	Msg.send(msgRet, human.fd)
end

-- 发送排行榜
function sendQuery(human, boardType)
	local board = BillboardDB.getBoard(boardType)
	if not board then return end

	local msgRet = Msg.gc.GC_BILLBOARD_QUERY
	msgRet.boardType = boardType
	msgRet.list[0] = math.min(#msgRet.list, #board.rank2data)
	for i = 1, msgRet.list[0] do
		local rankData = board.rank2data[i]
		fontBoardNet(msgRet.list[i], boardType, i, rankData)
	end

	local boardUuid = getBoardUuidByType(boardType, human)
	local myRank = BillboardDB.getRank(boardType, boardUuid)

	local myRankData = myRank and board.rank2data[myRank]
	if myRankData == nil then
		local value1, value2 = BRoleLogic.getValues(boardType, human.db)
		if value1 then
			myRankData = BRoleLogic.getCacheData(boardType, human.db, value1, value2)
		end
	end
	fontBoardNet(msgRet.ownData, boardType, myRank, myRankData, human.db._id)
	--Msg.trace(msgRet)
	Msg.send(msgRet, human.fd)
end


-- 清掉 每日重置的排行榜 
function onZero()
	for type in pairs(BillboardDefine.DAY_RESET) do 
		BillboardDB.initBoard(type)
	end
end

function isDot(human)
	for boardType, config in ipairs(BillboardExcel.board) do
		if config.isShow == 1 then
			if BillboardAim.isAimRed(human, boardType) then
               return true
            end
		end
	end
    return false
end